package com.raychi.demo.action;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.raychi.demo.entity.Custom;
import com.raychi.demo.service.CustomService;
import com.raychi.demo.util.CustomUtil;
import com.raychi.demo.util.JdbcUtil;

public class CustomAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private List<Custom> list;
	private Custom custom;
	private CustomService customService;
	
	public String list() throws ClassNotFoundException, SQLException{
		list = customService.getcustoms();
		return "list";
	}
	
	public String input(){
		
		return "input";
	}
	
	public String save() throws Exception{
		customService.save(custom);
		return list();
	}
	
	public String edit(){
		
		return "edit";
	}
	
	public String update() throws Exception{
		customService.update(custom);
		return list();
	}
	
	public String delete() throws Exception{
		customService.delete(custom);
		return list();
	}
	
	public Custom getCustomById(int id) {
		Custom custom = null;
		Connection conn = null;
		try {
			conn = JdbcUtil.getConnection();
			Statement st = conn.createStatement();
			String sql = "select * from custom where id=" + id;
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				custom = CustomUtil.createCustom(rs);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (conn != null)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		return custom;
		
	}


	public List<Custom> getList() {
		return list;
	}


	public void setList(List<Custom> list) {
		this.list = list;
	}


	public Custom getCustom() {
		return custom;
	}


	public void setCustom(Custom custom) {
		this.custom = custom;
	}

	public void setCustomService(CustomService customService) {
		this.customService = customService;
	}
}
